package com.teachoa.web;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.plugin.activerecord.Page;
import com.teachoa.bean.User;
import com.teachoa.bean.UserTask;
import com.teachoa.constant.Global;
import com.teachoa.enumtype.UserTypeEnum;
import com.teachoa.util.DictUtil;
import com.teachoa.util.IDUtil;
import com.teachoa.util.MD5Util;

public class UserController extends BaseController {
	
	public void index() {
		userPage();
		renderJsp("user.jsp");
	}
	
	public void turnpage() {
		userPage();
		renderJsp("user_table.jsp");
	}
	
	private void userPage() {
		Map<String, String> param = getParamMap();
		Page<User> userPage = User.dao.findUserByPage(param);
		Map<Integer, String> sexMap = DictUtil.getDictMapByPath(Global.sexPath);
		//Map<Integer, String> userTypeMap = DictUtil.getDictMapByPath(Global.userTypePath);
		//Map<Integer, String> gradeMap = DictUtil.getDictMapByPath(Global.gradePath);
		//Map<Integer, String> majorMap = DictUtil.getDictMapByPath(Global.majorPath);
		for(User user:userPage.getList()) {
			user.put("sex_display", sexMap.get(user.getInt("sex")));
			user.put("type_display", UserTypeEnum.getEnumByCode(user.getInt("type")).getDesc());
			//user.put("grade_display", gradeMap.get(user.getInt("grade")));
			//user.put("major_display", majorMap.get(user.getInt("major")));
		}
		setAttr("userPage", userPage);
	}
	
	public void delete() {
		String id = getPara();
		try {
			if(UserTask.dao.checkUserInTask(id)) {
				renderJson(fail("正在进行项目中的人员信息不能删除！！"));
				return;
			}
			User.dao.deleteById(id);
			renderJson(success("删除成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("删除失败！！"));
		}
	}
	
	public void save() {
		User user = getModel(User.class);
		String id = user.getStr("id");
		boolean isAdd = StringUtils.isBlank(id);
		if(isAdd) {
			user.set("id", IDUtil.getId());
			user.set("password", MD5Util.md5(Global.password));
			user.set("create_time", new Date());
		}
		try {
			String msg = "保存成功！！";
			if(isAdd) {
				user.save();
				msg+="初始密码为123456";
			} else {
				user.update();
			}
			renderJson(success(msg));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("保存失败！！"));
		}
	}
	
	public void load() {
		String id = getPara();
		if(StringUtils.isNotBlank(id)) 
			setAttr("user", User.dao.findById(id));
		//User user = User.dao.findById(getPara());
		//System.err.println(DictUtil.getSubDictsByPath(Global.sexPath));
		setAttr("sexs", DictUtil.getSubDictsByPath(Global.sexPath));
		//setAttr("userTypes", DictUtil.getSubDictsByPath(Global.userTypePath));
		setAttr("grades", DictUtil.getSubDictsByPath(Global.gradePath));
		setAttr("majors", DictUtil.getSubDictsByPath(Global.majorPath));
		String oper = getPara("oper");
		if("view".equals(oper))
			renderJsp("user_view.jsp");
		else {
			renderJsp("user_add.jsp");
		}
	}
	
	public void pwd() {
		String oldpwd = MD5Util.md5(getPara("oldpwd"));
		String newpwd = MD5Util.md5(getPara("newpwd"));
		User user = getSessionAttr("user");
		if(oldpwd.equals(user.getStr("password"))) {
			user.set("password", newpwd);
			user.update();
			removeSessionAttr("user");
			renderJson(success("密码修改成功！！请重新登陆"));
		} else {
			renderJson(fail("密码输入错误！！"));
		}
	}
	
}
